/*
 * Created by SharpDevelop.
 * User: phtrung
 * Date: 12/30/2009
 * Time: 2:23 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections.Generic;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
using popLib;
using System.Data;
using System.Threading;
using System.IO;
using System.Net;

namespace PHPUploader
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		
		Thread uploadThread;
		DataTable dt_file;
		string url = "";
		string tag = "";
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			dt_file = DataLib.initDataTable( "Files[ name, size, state, path ]" );
			dg_file.DataSource = dt_file;
			
			FormLib.ExecuteDragAndDrop handleDragItem = delegate(Control control, string[] files) {
				importFile(files);				
			};
			
			FormLib.dragAndDrop( this, handleDragItem );
			FormLib.dragAndDrop( dg_file, handleDragItem );
			FormLib.dragAndDrop( groupBox1, handleDragItem );
			FormLib.dragAndDrop( Server, handleDragItem );
			Dg_fileResize( null, null );
			
		}
		
		void Button1Click(object sender, EventArgs e)
		{
			url = cbx_url.Text;
				tag = txt_tagname.Text;
				uploadThread = new Thread( new ThreadStart(multiUpload) );
				uploadThread.IsBackground = true;
				uploadThread.Start();
		}
		
		void multiUpload(){
			string file;
			ReturnSet result;
			foreach( DataRow row in dt_file.Rows ){
				file = row[3].ToString();
				row[1] = "Uploading";
				//result = NetLib.uploadFileHttp( file, url, null, tag, null );
				result = NetLib.uploadFile( url, file );				
				row[1] = result.Success ? "Uploaded" : "Fail" ;				
			}
		}	
		
		void Button2Click(object sender, EventArgs e)
		{
			string[] files = FormLib.openMultiFile("All files(*.*)|*.*");
			if( files != null ){
				importFile(files);
			}
		}
		
		void importFile( string[] files ){
			foreach( string file in files ){
				dt_file.Rows.Add( Path.GetFileName(file), NumberLib.getDouble( NumberLib.getDouble(new FileInfo(file).Length)/1024, 2 ) + "kb", "Ready", file );
			}
		}
		
		void Dg_fileSizeChanged(object sender, EventArgs e)
		{
			Column1.Width = 120;
			Column2.Width = Column3.Width = 70;
			Column3.Width = dg_file.Width - Column1.Width - Column2.Width - Column3.Width;			
		}
		
		void Dg_fileResize(object sender, EventArgs e)
		{
			Column1.Width = 120;
			Column2.Width = Column3.Width = 70;
			Column3.Width = dg_file.Width - Column1.Width - Column2.Width - Column3.Width;			
		}		
	}
}
